﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using HiLand.Utility.Setting;
using HiLand.Utility.Web;
using GBFinance.Entity;
using GBFinance.BLL;
using HiLand.General.Entity;
using HiLand.General.BLL;
using HiLand.General;
using HiLand.Utility4.MVC;

namespace GBFinance.Web.Models
{
    public class Miscs
    {
        private static IFormatProvider currentCultureInfo = null;
        /// <summary>
        /// 系统使用的文化信息
        /// </summary>
        public static IFormatProvider CurrentCultureInfo
        {
            get
            {
                if (currentCultureInfo == null)
                {
                    string usingCultureInfoString = Config.GetAppSetting("usingCultureInfo");
                    if (string.IsNullOrWhiteSpace(usingCultureInfoString))
                    {
                        usingCultureInfoString = "en-AU";
                    }

                    currentCultureInfo = new CultureInfo(usingCultureInfoString);
                }

                return currentCultureInfo;
            }
        }

        private static string dateTimeFormating = string.Empty;
        /// <summary>
        /// 系统使用的日期格式
        /// </summary>
        public static string DateTimeFormating
        {
            get
            {
                if (dateTimeFormating == string.Empty)
                {
                    dateTimeFormating = Config.GetAppSetting("dateTimeFormating");
                }

                if (string.IsNullOrWhiteSpace(dateTimeFormating))
                {
                    dateTimeFormating = "dd/MM/yyyy";
                }

                return dateTimeFormating;
            }
        }

        //public static LoanSimpleInfo CurrentSecuredLoanInfo
        //{
        //    get { return CurrentLoanSimpleInfo(LoanTypes.Secured); }
        //}

        //public static LoanSimpleInfo CurrentUnSecuredLoanInfo
        //{
        //    get { return CurrentLoanSimpleInfo(LoanTypes.UnSecured); }
        //}

        #region 常用配置
        private static int pageSizeManage = 0;
        /// <summary>
        /// 后台管理中信息列表时，每页显示的信息数
        /// </summary>
        public static int PageSizeManage
        {
            get
            {
                if (pageSizeManage == 0)
                {
                    pageSizeManage = Config.GetAppSettingInt("PageSizeManage",10);
                }
                return pageSizeManage;
            }
        }

        private static int pageSizeFront = 0;
        /// <summary>
        /// 后台管理中信息列表时，每页显示的信息数
        /// </summary>
        public static int PageSizeFront
        {
            get
            {
                if (pageSizeFront == 0)
                {
                    pageSizeFront = Config.GetAppSettingInt("PageSizeFront",10);
                }
                return pageSizeFront;
            }
        }
        #endregion

        /// <summary>
        /// 当前贷款的简单信息
        /// </summary>
        public static LoanSimpleInfo CurrentLoanSimpleInfo
        {
            get
            {
                Guid loanGuid = Guid.Empty;
                Guid enterpriseGuid = Guid.Empty;
                //1.通过请求参数获取信息（主要用于后台管理）
                string adminLoanGuid = RequestHelper.GetValue("adminLoanGuid");
                if (string.IsNullOrWhiteSpace(adminLoanGuid) == false && adminLoanGuid != Guid.Empty.ToString())
                {
                    loanGuid = new Guid(adminLoanGuid);
                }

                string adminEnterpriseGuid = RequestHelper.GetValue("adminEnterpriseGuid");
                if (string.IsNullOrWhiteSpace(adminEnterpriseGuid) == false && adminEnterpriseGuid != Guid.Empty.ToString())
                {
                    enterpriseGuid = new Guid(adminEnterpriseGuid);
                }
                else
                {
                    if (loanGuid != Guid.Empty)
                    {
                        LoanBasicEntity loanEntity = LoanBasicBLL.Get(loanGuid);
                        if (loanEntity != null)
                        {
                            enterpriseGuid = loanEntity.LoanUserID;
                        }
                    }
                }


                //2.通过Cookie获取信息（主要用于客户的贷款流程）
                LoanCookie loanCookie = null;
                LoanTypes loanType = GetCurrentLoanType();
                if (loanType == LoanTypes.Secured)
                {
                    loanCookie = CookieInfo.Load<SecuredLoanCookie>();
                }
                else
                {
                    loanCookie = CookieInfo.Load<UnSecuredLoanCookie>();
                }

                string enterpriseGuidString = loanCookie.EnterpriseGuid;
                string loandGuidString = loanCookie.LoanGuid;

                if (enterpriseGuid == Guid.Empty && string.IsNullOrWhiteSpace(enterpriseGuidString) == false && enterpriseGuidString != Guid.Empty.ToString())
                {
                    enterpriseGuid = new Guid(enterpriseGuidString);
                }

                if (loanGuid == Guid.Empty && string.IsNullOrWhiteSpace(loandGuidString) == false && loandGuidString != Guid.Empty.ToString())
                {
                    loanGuid = new Guid(loandGuidString);
                }

                return new LoanSimpleInfo(loanGuid, enterpriseGuid);
            }
        }

        /// <summary>
        /// 在cookie中设置贷款的步骤数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="step"></param>
        public static void SetLoanStep<T>(int step) where T : LoanCookie, new()
        {
            LoanCookie loanCookie = CookieInfo.Load<T>();
            bool isNeedSaveCookie = false;
            if (loanCookie.CurrentStep < step)
            {
                loanCookie.CurrentStep = step;
                isNeedSaveCookie = true;
            }

            if (isNeedSaveCookie == true)
            {
                loanCookie.Save();
            }
        }

        /// <summary>
        /// 重置LoanCookie
        /// </summary>
        public static void ResetLoanCookie<T>() where T : LoanCookie, new()
        {
            LoanCookie loanCookie = CookieInfo.Load<T>();
            loanCookie.CurrentStep = 0;
            loanCookie.EnterpriseGuid = string.Empty;
            loanCookie.LoanGuid = string.Empty;
            loanCookie.Save();
        }

        /// <summary>
        /// 创建新的贷款Cookie
        /// </summary>
        /// <param name="isAlwaysCreateNew">是否总是创建新的Cookie（即原来存在的情况下会覆盖）</param>
        public static void CreateNewLoanCookie<T>(bool isAlwaysCreateNew = false) where T : LoanCookie, new()
        {
            LoanCookie loanCookie = CookieInfo.Load<T>();
            if (loanCookie.CheckIsNewBirthLoan() == true || isAlwaysCreateNew == true)
            {
                loanCookie.EnterpriseGuid = Guid.NewGuid().ToString();
                loanCookie.LoanGuid = Guid.NewGuid().ToString();
                loanCookie.Save();
            }
        }

        /// <summary>
        /// 获取当前执行的贷款的类型
        /// </summary>
        /// <returns></returns>
        public static LoanTypes GetCurrentLoanType()
        {
            LoanTypes loanType = LoanTypes.UnSecured;
            string currentControllerName= MVCHelper.GetCurrentControllerName().ToLower();
            if (currentControllerName == "secured" ||currentControllerName== "securedloan")
            {
                loanType = LoanTypes.Secured;
            }
            else
            {
                loanType = LoanTypes.UnSecured;
            }

            return loanType;
        }
    }
}